{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Make Number of Distinct Characters Equal"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #string #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #字符串 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: isItPossible"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #使字符串总不同字符的数目相等"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你两个下标从 <strong>0</strong> 开始的字符串 <code>word1</code> 和 <code>word2</code> 。</p>\n",
    "\n",
    "<p>一次 <strong>移动</strong> 由以下两个步骤组成：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>选中两个下标&nbsp;<code>i</code> 和 <code>j</code> ，分别满足 <code>0 &lt;= i &lt; word1.length</code> 和 <code>0 &lt;= j &lt; word2.length</code> ，</li>\n",
    "\t<li>交换 <code>word1[i]</code> 和 <code>word2[j]</code> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p>如果可以通过 <strong>恰好一次</strong> 移动，使 <code>word1</code> 和 <code>word2</code> 中不同字符的数目相等，则返回 <code>true</code> ；否则，返回 <code>false</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>word1 = \"ac\", word2 = \"b\"\n",
    "<strong>输出：</strong>false\n",
    "<strong>解释：</strong>交换任何一组下标都会导致第一个字符串中有 2 个不同的字符，而在第二个字符串中只有 1 个不同字符。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>word1 = \"abcc\", word2 = \"aab\"\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>交换第一个字符串的下标 2 和第二个字符串的下标 0 。之后得到 word1 = \"abac\" 和 word2 = \"cab\" ，各有 3 个不同字符。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>word1 = \"abcde\", word2 = \"fghij\"\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>无论交换哪一组下标，两个字符串中都会有 5 个不同字符。</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>word1</code> 和 <code>word2</code> 仅由小写英文字母组成。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [make-number-of-distinct-characters-equal](https://leetcode.cn/problems/make-number-of-distinct-characters-equal/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [make-number-of-distinct-characters-equal](https://leetcode.cn/problems/make-number-of-distinct-characters-equal/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"ac\"\\n\"b\"', '\"abcc\"\\n\"aab\"', '\"abcde\"\\n\"fghij\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        cnt1, cnt2 = Counter(word1), Counter(word2)\n",
    "\n",
    "        for i in range(26):\n",
    "            for j in range(26):\n",
    "                a = chr(ord('a') + i)\n",
    "                b = chr(ord('a') + j)\n",
    "                t1, t2 = cnt1.copy(), cnt2.copy()\n",
    "                if t1[a] > 0 and t2[b] > 0: \n",
    "                    t1[a] -= 1\n",
    "                    t2[a] += 1 \n",
    "                    t2[b] -= 1\n",
    "                    t1[b] += 1\n",
    "                    if t1[a] == 0: del t1[a]\n",
    "                    if t2[b] == 0: del t2[b]\n",
    "                    if len(t1) == len(t2):\n",
    "                        # print(t1, t2)\n",
    "                        return True\n",
    "        \n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        cnt1, cnt2 = Counter(word1), Counter(word2)\n",
    "\n",
    "        for i in range(26):\n",
    "            for j in range(26):\n",
    "                a = chr(ord('a') + i)\n",
    "                b = chr(ord('a') + j)\n",
    "                if cnt1[a] > 0 and cnt2[b] > 0: \n",
    "                    t1, t2 = cnt1.copy(), cnt2.copy()\n",
    "                    t1[a] -= 1\n",
    "                    t2[a] += 1 \n",
    "                    t2[b] -= 1\n",
    "                    t1[b] += 1\n",
    "                    if t1[a] == 0: del t1[a]\n",
    "                    if t2[b] == 0: del t2[b]\n",
    "                    if len(t1) == len(t2):\n",
    "                        # print(t1, t2)\n",
    "                        return True\n",
    "        \n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        from collections import defaultdict\n",
    "        cnt_word1 = defaultdict(int)\n",
    "        cnt_word2 = defaultdict(int)\n",
    "        cnt_word11 = defaultdict(int)\n",
    "        cnt_word22 = defaultdict(int)\n",
    "        \n",
    "        for i in range(len(word1)):\n",
    "            cnt_word1[word1[i]] += 1\n",
    "            cnt_word11[word1[i]] += 1\n",
    "        \n",
    "        for i in range(len(word2)):\n",
    "            cnt_word2[word2[i]] += 1\n",
    "            cnt_word22[word2[i]] += 1\n",
    "        \n",
    "        \n",
    "        for k1 in cnt_word1.keys():\n",
    "            for k2 in cnt_word2.keys():\n",
    "                # 交换\n",
    "                cnt_word11[k1] -= 1\n",
    "                cnt_word11[k2] += 1\n",
    "                \n",
    "                cnt_word22[k1] += 1\n",
    "                cnt_word22[k2] -= 1\n",
    "                \n",
    "                # 计数\n",
    "                diff1 = sum([1 if x != 0 else 0 for x in cnt_word11.values()])\n",
    "                diff2 = sum([1 if x != 0 else 0 for x in cnt_word22.values()])\n",
    "                if diff1 == diff2:\n",
    "                    # print(\"diff1, diff2: \", diff1, diff2)\n",
    "                    return True\n",
    "                \n",
    "                # 恢复\n",
    "                cnt_word11[k1] += 1\n",
    "                cnt_word11[k2] -= 1\n",
    "                \n",
    "                cnt_word22[k1] -= 1\n",
    "                cnt_word22[k2] += 1\n",
    "                \n",
    "        \n",
    "        return False\n",
    "                \n",
    "                \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        # 统计每个字母出现的次数\n",
    "        c1, c2 = Counter(word1), Counter(word2)\n",
    "        for x, c in c1.items():\n",
    "            for y, d in c2.items():\n",
    "                if y == x:  # 无变化\n",
    "                    if len(c1) == len(c2): return True\n",
    "                elif len(c1) - (c == 1) + (y not in c1) == \\\n",
    "                     len(c2) - (d == 1) + (x not in c2):  # 基于长度计算变化量\n",
    "                    return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        c1 = Counter(word1)\n",
    "        c2 = Counter(word2)\n",
    "        for x, c in c1.items():\n",
    "            for y, k in c2.items():\n",
    "                if x==y:\n",
    "                    if len(c1)==len(c2):\n",
    "                        return True\n",
    "                elif len(c1) - (c==1) + (y not in c1) == len(c2) - (k==1) + (x not in c2):\n",
    "                    return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        c1 = Counter(word1)\n",
    "        c2 = Counter(word2)\n",
    "        for x,c in c1.items():\n",
    "            for y,d in c2.items():\n",
    "                if x == y:\n",
    "                    if len(c1) == len(c2):\n",
    "                        return True\n",
    "                elif len(c1) - (c == 1) + (y not in c1) == len(c2) - (d == 1) + (x not in c2):\n",
    "                    return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        count1 =collections.Counter(word1)\n",
    "        count2 = collections.Counter(word2)\n",
    "        l1 = len(count1)\n",
    "        l2 = len(count2)\n",
    "        for x,c1 in count1.items():\n",
    "            for y ,c2 in count2.items():\n",
    "                if x == y:\n",
    "                    if l1 == l2 :return True\n",
    "                elif l1 - (c1 == 1) + (y not in count1) == l2 -(c2 == 1) + (x not in count2):\n",
    "                    return True\n",
    "        return False\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        # dic1 = [0]*26\n",
    "        # dic2 = [0]*26\n",
    "\n",
    "        # for w in word1:\n",
    "        #     dic1[ord(w) - ord('a')] += 1\n",
    "        # for w in word2:\n",
    "        #     dic2[ord(w) - ord('a')] += 1\n",
    "        # m = len(word1)\n",
    "        # n = len(word2)\n",
    "        # cnt1 = n-dic1.count(0)\n",
    "        # cnt2 = n-dic2.count(0)\n",
    "        # # if cnt1 == cnt2:\n",
    "        # #     return True\n",
    "\n",
    "        # for i in range(m):\n",
    "        #     s = ord(word1[i]) - ord('a')\n",
    "        #     tmpcnt1 = cnt1\n",
    "        #     if dic1[s] == 1:\n",
    "        #         tmpcnt1 -= 1\n",
    "\n",
    "        #     for j in range(n):\n",
    "        #         tmpcnt1_1 = tmpcnt1\n",
    "        #         t = ord(word2[j]) - ord('a')\n",
    "        #         if dic1[s] == dic2[t] :\n",
    "        #             if cnt1 == cnt2:\n",
    "        #                 return True \n",
    "        #             continue\n",
    "        #         tmpcnt2 = cnt2\n",
    "        #         if dic2[s] == 0:\n",
    "        #             tmpcnt2 += 1 \n",
    "        #         if dic2[t] == 1:\n",
    "        #             tmpcnt2 -= 1\n",
    "        #         if dic1[t] == 0:\n",
    "        #             tmpcnt1_1 += 1\n",
    "        #         if tmpcnt1_1 == tmpcnt2:\n",
    "        #             return True\n",
    "        # return False\n",
    "\n",
    "                    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        cnt1=Counter(word1)\n",
    "        cnt2=Counter(word2)\n",
    "        #\"abcc\" \"aab\"\n",
    "        for x,c in cnt1.items():\n",
    "            for y,d in cnt2.items():\n",
    "                if x==y:\n",
    "                    if len(cnt1)==len(cnt2):return True\n",
    "                elif len(cnt1)-(c==1)+(x in cnt2)==len(cnt2)-(d==1)+(y in cnt1):\n",
    "                    return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        w1, w2 = Counter(word1), Counter(word2)\n",
    "        for c1, n1 in w1.items():\n",
    "            for c2, n2 in w2.items():\n",
    "                if c1 == c2:\n",
    "                    if len(w1) == len(w2): return True\n",
    "                elif len(w1) - (n1 == 1) + (c2 not in w1) == len(w2) - (n2 == 1) + (c1 not in w2):\n",
    "                    return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        # 暴力也就26 * 26种可能\n",
    "        c1 = [0] * 26\n",
    "        for ch in word1:\n",
    "            c1[ord(ch) - ord('a')] += 1\n",
    "        c2 = [0] * 26\n",
    "        for ch in word2:\n",
    "            c2[ord(ch) - ord('a')] += 1\n",
    "        for i in range(26):\n",
    "            for j in range(26):\n",
    "                if not c1[i] or not c2[j]:\n",
    "                    continue\n",
    "                c1[i] -= 1\n",
    "                c2[i] += 1\n",
    "                c1[j] += 1\n",
    "                c2[j] -= 1\n",
    "                if sum(x != 0 for x in c1) == sum(x != 0 for x in c2):\n",
    "                    return True\n",
    "                c1[i] += 1\n",
    "                c2[i] -= 1\n",
    "                c1[j] -= 1\n",
    "                c2[j] += 1\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        c1 = Counter(word1)\n",
    "        c2 = Counter(word2)\n",
    "        for x1, y1 in c1.items():\n",
    "            for x2, y2 in c2.items():\n",
    "                if x1 == x2:\n",
    "                    if len(c1) == len(c2): return True\n",
    "                elif len(c1) - (y1 == 1) + (x2 not in c1) == len(c2) - (y2 == 1) + (x1 not in c2):\n",
    "                    return True\n",
    "        return False\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        c1, c2 = Counter(word1), Counter(word2)\n",
    "        for x, c in c1.items():\n",
    "            for y, d in c2.items():\n",
    "                if y == x:  # 无变化\n",
    "                    if len(c1) == len(c2): return True\n",
    "                elif len(c1) - (c == 1) + (y not in c1) == len(c2) - (d == 1) + (x not in c2):  # 基于长度计算变化量\n",
    "                    return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        cnt1 = Counter(word1)\n",
    "        cnt2 = Counter(word2)\n",
    "        lst1 = list(cnt1.keys())\n",
    "        lst2 = list(cnt2.keys())\n",
    "        for w in lst1:\n",
    "            for x in lst2:\n",
    "                cnt1[w] -= 1\n",
    "                cnt1[x] += 1\n",
    "                cnt2[w] += 1\n",
    "                cnt2[x] -= 1\n",
    "                \n",
    "                if sum(va>0 for va in cnt1.values()) == sum(va>0 for va in cnt2.values()):\n",
    "                    return True\n",
    "                cnt1[w] += 1\n",
    "                cnt1[x] -= 1\n",
    "                cnt2[w] -= 1\n",
    "                cnt2[x] += 1\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        m, n = len(word1), len(word2)\n",
    "        cnt1, cnt2 = Counter(word1), Counter(word2)\n",
    "        for c1, v1 in cnt1.items():\n",
    "            for c2, v2 in cnt2.items():\n",
    "                if c1 == c2:\n",
    "                    if len(cnt1) == len(cnt2): return True\n",
    "                elif len(cnt1) - (v1 == 1) + (c2 not in cnt1) == len(cnt2) - (v2 == 1) + (c1 not in cnt2):\n",
    "                    return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        a, b = Counter(word1), Counter(word2)\n",
    "        for k1, v1 in a.items():\n",
    "            for k2, v2 in b.items():\n",
    "                if k1 == k2:\n",
    "                    if len(a) == len(b): return True\n",
    "                elif len(a) - (v1 == 1) + (k2 not in a) == len(b) - (v2 == 1) + (k1 not in b):\n",
    "                    return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        cnt1 =[0] * 26\n",
    "        cnt2 = [0] * 26\n",
    "        for c in word1:\n",
    "            cnt1[ord(c) - ord('a')] += 1\n",
    "        for c in word2:\n",
    "            cnt2[ord(c) - ord('a')] += 1\n",
    "        f1 = f2 = 0\n",
    "        for i in range(26):\n",
    "            f1 += 1 if cnt1[i] else 0\n",
    "            f2 += 1 if cnt2[i] else 0\n",
    "        if abs(f1 - f2) > 2:\n",
    "            return False\n",
    "        for i in range(26):\n",
    "            if not cnt1[i]:\n",
    "                continue\n",
    "            for j in range(26):\n",
    "                if not cnt2[j]:\n",
    "                    continue\n",
    "                if i == j:\n",
    "                    if f1 == f2:\n",
    "                        return True\n",
    "                    continue\n",
    "                v1, v2 = f1, f2\n",
    "                if cnt1[i] - 1 == 0:\n",
    "                    v1 -= 1\n",
    "                if not cnt1[j]:\n",
    "                    v1 += 1\n",
    "                if cnt2[j] - 1 == 0:\n",
    "                    v2 -= 1\n",
    "                if not cnt2[i]:\n",
    "                    v2 += 1\n",
    "                if v1 == v2:\n",
    "                    return True\n",
    "        return False\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        m, n = len(word1), len(word2)\n",
    "        cnt1, cnt2 = Counter(word1), Counter(word2)\n",
    "        for c1, v1 in cnt1.items():\n",
    "            for c2, v2 in cnt2.items():\n",
    "                q, w = cnt1.copy(), cnt2.copy()\n",
    "                q[c1] -= 1\n",
    "                if q[c1] == 0: del q[c1]\n",
    "                q[c2] += 1\n",
    "                w[c1] += 1\n",
    "                w[c2] -= 1\n",
    "                if w[c2] == 0: del w[c2]\n",
    "                if len(q) == len(w):\n",
    "                    return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        c1, c2 = Counter(word1), Counter(word2)\n",
    "        t1, t2 = len(c1), len(c2)\n",
    "        if t1 == t2 and len(set(c1)&set(c2)): return True\n",
    "        if abs(t1 - t2) > 2: return False\n",
    "        for k1, k2 in product(c1, c2):\n",
    "            if k1 == k2: continue\n",
    "            if t1 - (c1[k1]==1) + (c1[k2] == 0) == t2 - (c2[k2]==1) + (c2[k1] == 0):\n",
    "                return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        c1, c2 = Counter(word1), Counter(word2)\n",
    "        for x, c in c1.items():\n",
    "            for y, d in c2.items():\n",
    "                if x == y:\n",
    "                    if len(c1) == len(c2):\n",
    "                        return True\n",
    "                elif len(c1) - (c==1) + (y not in c1) == len(c2) - (d==1) + (x not in c2):\n",
    "                    return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        c1 = Counter(word1)\n",
    "        c2 = Counter(word2)\n",
    "\n",
    "        for l, c in c1.items():\n",
    "            for r, d in c2.items():\n",
    "                if l == r and len(c1) == len(c2):\n",
    "                    return True \n",
    "                elif l!= r and  len(c1) - (c1[l]== 1) + (not (r in c1)) == len(c2) - (c2[r]== 1) + (not (l in c2)):\n",
    "                    return True\n",
    "                \n",
    "        return False\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        c1=Counter(word1)\n",
    "        c2=Counter(word2)\n",
    "        for w1,i in c1.items():\n",
    "            for w2,j in c2.items():\n",
    "                if w1==w2:\n",
    "                    if len(c1)==len(c2):\n",
    "                        return True\n",
    "                elif len(c1)-(i==1)+(w2 not in c1)==len(c2)-(j==1)+(w1 not in c2):\n",
    "                    return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        c1 = Counter(word1)\n",
    "        c2 = Counter(word2)\n",
    "        # 考虑到word1和word2仅仅由小写字母组成,且移动只有一次\n",
    "        # 那么可以考虑通过暴力枚举的方式来求解\n",
    "        # 设定word1中取出的字符是x, word2中取出的字符是y\n",
    "        # 枚举的时候有两种情况， \n",
    "        # 如果两个字符一样， x==y, 那么只有当两个哈希表的长度一致时，不同字符的数目相等\n",
    "        # 注意 如果x==y, 长度不一致，也有可能存在交换其它字符，使得长度一致, 应该进行下一次枚举\n",
    "        # 如果x!=y, \n",
    "        # 1. 如果x出现的次数为1，那么len1的长度要减少1\n",
    "        # 2. 如果x出现的次数>=1, 那么len1的长度不变\n",
    "        # 3. y如果在x中没有出现过，len1的长度加1， 出现过，len1的长度不变\n",
    "        for x, c in c1.items():\n",
    "            for y, d in c2.items():\n",
    "                len1 = len(c1)\n",
    "                len2 = len(c2)\n",
    "                if x == y:\n",
    "                    if len1 == len2:\n",
    "                        return True\n",
    "                else:\n",
    "                    if c == 1:\n",
    "                        len1 -= 1\n",
    "                    if y not in c1:\n",
    "                        len1 += 1\n",
    "                    if d == 1:\n",
    "                        len2 -= 1\n",
    "                    if x not in c2:\n",
    "                        len2 += 1\n",
    "                    if len1 == len2:\n",
    "                        return True\n",
    "        \n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        hash1=collections.Counter(word1)\n",
    "        hash2=collections.Counter(word2)\n",
    "        for key1,value1 in hash1.items():\n",
    "            for key2,value2 in hash2.items():\n",
    "                if key1==key2:\n",
    "                    if len(hash1)==len(hash2):\n",
    "                        return True \n",
    "                else:\n",
    "                    if len(hash1)-(value1==1)+(key2 not in hash1)==len(hash2)-(value2==1)+(key1 not in hash2):\n",
    "                        return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        c1 = Counter(word1)\n",
    "        c2 = Counter(word2)\n",
    "        for k1, v1 in c1.items():\n",
    "            for k2, v2 in c2.items():\n",
    "                if k1 == k2:\n",
    "                    if len(c1) == len(c2):\n",
    "                        return True\n",
    "                else:\n",
    "                    x1 = len(c1)\n",
    "                    if v1 == 1:\n",
    "                        x1 -= 1\n",
    "                    x2 = len(c2)\n",
    "                    if v2 == 1:\n",
    "                        x2 -= 1\n",
    "                    if k2 not in c1:\n",
    "                        x1 += 1\n",
    "                    if k1 not in c2:\n",
    "                        x2 += 1\n",
    "                    if x1 == x2:\n",
    "                        return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        c1 = Counter(word1)\n",
    "        c2 = Counter(word2)\n",
    "        \n",
    "        for k1, v1 in c1.items():\n",
    "            for k2, v2 in c2.items():\n",
    "                num1, num2 = len(c1), len(c2)\n",
    "                if k1 == k2:\n",
    "                    if len(c1) == len(c2):\n",
    "                        return True\n",
    "                    continue\n",
    "                if k1 not in c2:\n",
    "                    num2 += 1\n",
    "                if v1 == 1:\n",
    "                    num1 -= 1\n",
    "                if k2 not in c1:\n",
    "                    num1 += 1\n",
    "                if v2 == 1:\n",
    "                    num2 -= 1\n",
    "                if num1 == num2:\n",
    "                    return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        cnt1, cnt2 = collections.Counter(word1), collections.Counter(word2)\n",
    "        n1, n2 = len(cnt1), len(cnt2)\n",
    "        for c1 in cnt1:\n",
    "            for c2 in cnt2:\n",
    "                if c1 == c2:\n",
    "                    if n1 == n2:\n",
    "                        return True\n",
    "                else:\n",
    "                    tmp1 = n1 - (cnt1[c1] == 1) + (c2 not in cnt1)\n",
    "                    tmp2 = n2 - (cnt2[c2] == 1) + (c1 not in cnt2)\n",
    "                    if tmp1 == tmp2:\n",
    "                        return True\n",
    "        return False\n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        c1, c2 = Counter(word1), Counter(word2)\n",
    "        for x, c in c1.items():\n",
    "            for y, d in c2.items():\n",
    "                if y == x:  # 无变化\n",
    "                    if len(c1) == len(c2): return True\n",
    "                elif len(c1) - (c == 1) + (y not in c1) == \\\n",
    "                     len(c2) - (d == 1) + (x not in c2):  # 基于长度计算变化量\n",
    "                    return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        c1 = Counter(word1)\n",
    "        c2 = Counter(word2)\n",
    "        for x, m in c1.items():\n",
    "            for y, n in c2.items():\n",
    "                if x == y:\n",
    "                    if len(c1) == len(c2): return True\n",
    "                elif len(c1)-(m==1)+(y not in c1) == len(c2)-(n==1)+(x not in c2):\n",
    "                    return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\r\n",
    "        c1 = Counter(word1)\r\n",
    "        c2 = Counter(word2)\r\n",
    "        # if abs(len(c1) - len(c2)) > 2: return False\r\n",
    "        for x, c in c1.items():\r\n",
    "            for y, d in c2.items():\r\n",
    "                if x == y:\r\n",
    "                    if len(c1) == len(c2):\r\n",
    "                        return True\r\n",
    "                else:\r\n",
    "                    if len(c1) - (c == 1) + (y not in c1) == len(c2) - (d == 1) + (x not in c2):\r\n",
    "                        return True\r\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isItPossible(self, word1: str, word2: str) -> bool:\n",
    "        m1={}\n",
    "        m2={}\n",
    "        for i in word1:\n",
    "            if m1.get(i)==None:\n",
    "                m1[i]=0\n",
    "            m1[i]+=1\n",
    "        \n",
    "        for i in word2:\n",
    "            if m2.get(i)==None:\n",
    "                m2[i]=0\n",
    "            m2[i]+=1\n",
    "\n",
    "        \n",
    "\n",
    "        \n",
    "\n",
    "        for key1 in m1:\n",
    "            for key2 in m2:\n",
    "                m=len(m1)\n",
    "                n=len(m2)\n",
    "                if key1==key2:\n",
    "                    if m==n:\n",
    "                        return True\n",
    "                    continue\n",
    "\n",
    "                if m1[key1]==1:\n",
    "                    m-=1\n",
    "                if m2[key2]==1:\n",
    "                    n-=1\n",
    "                if m1.get(key2)==None:\n",
    "                    m+=1\n",
    "                if m2.get(key1)==None:\n",
    "                    n+=1\n",
    "                \n",
    "                if m==n:\n",
    "                    return True\n",
    "        return False\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
